<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="配送订单号" prop="orderNo">
        <el-input v-model="formData.orderNo" placeholder="请输入配送订单号" />
      </el-form-item>
      <el-form-item label="配送基地ID" prop="farmId">
        <el-input v-model="formData.farmId" placeholder="请输入配送基地ID" />
      </el-form-item>
      <el-form-item label="配送菜场ID" prop="storeId">
        <el-input v-model="formData.storeId" placeholder="请输入配送菜场ID" />
      </el-form-item>
      <el-form-item label="配送总价" prop="totalPrice">
        <el-input v-model="formData.totalPrice" placeholder="请输入配送总价" />
      </el-form-item>
      <el-form-item label="车牌号" prop="vehiclePlate">
        <el-input v-model="formData.vehiclePlate" placeholder="请输入车牌号" />
      </el-form-item>
      <el-form-item label="配送物流公司" prop="logisticsCompany">
        <el-input v-model="formData.logisticsCompany" placeholder="请输入配送物流公司" />
      </el-form-item>
      <el-form-item label="配送物流单号" prop="logisticsNo">
        <el-input v-model="formData.logisticsNo" placeholder="请输入配送物流单号" />
      </el-form-item>
      <el-form-item label="预计到货时间" prop="estimatedArrivalTime">
        <el-date-picker
          v-model="formData.estimatedArrivalTime"
          type="date"
          value-format="x"
          placeholder="选择预计到货时间"
        />
      </el-form-item>
      <el-form-item label="实际出发时间" prop="actualDepartureTime">
        <el-date-picker
          v-model="formData.actualDepartureTime"
          type="date"
          value-format="x"
          placeholder="选择实际出发时间"
        />
      </el-form-item>
      <el-form-item label="实际到达时间" prop="actualArrivalTime">
        <el-date-picker
          v-model="formData.actualArrivalTime"
          type="date"
          value-format="x"
          placeholder="选择实际到达时间"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="配送状态" prop="deliveryStatus">
        <el-radio-group v-model="formData.deliveryStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="预警状态" prop="alertStatus">
        <el-radio-group v-model="formData.alertStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="预警时间" prop="alertTime">
        <el-date-picker
          v-model="formData.alertTime"
          type="date"
          value-format="x"
          placeholder="选择预警时间"
        />
      </el-form-item>
      <el-form-item label="备注信息" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注信息" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { OrderApi, Order } from '@/api/mall/trade/deliveryOrder'

/** 农场到自营门店的配送订单 表单 */
defineOptions({ name: 'OrderForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  orderNo: undefined,
  farmId: undefined,
  storeId: undefined,
  totalPrice: undefined,
  vehiclePlate: undefined,
  logisticsCompany: undefined,
  logisticsNo: undefined,
  estimatedArrivalTime: undefined,
  actualDepartureTime: undefined,
  actualArrivalTime: undefined,
  status: undefined,
  deliveryStatus: undefined,
  alertStatus: undefined,
  alertTime: undefined,
  remark: undefined,
})
const formRules = reactive({
  orderNo: [{ required: true, message: '配送订单号不能为空', trigger: 'blur' }],
  farmId: [{ required: true, message: '配送基地ID不能为空', trigger: 'blur' }],
  storeId: [{ required: true, message: '配送菜场ID不能为空', trigger: 'blur' }],
  totalPrice: [{ required: true, message: '配送总价不能为空', trigger: 'blur' }],
  vehiclePlate: [{ required: true, message: '车牌号不能为空', trigger: 'blur' }],
  logisticsCompany: [{ required: true, message: '配送物流公司不能为空', trigger: 'blur' }],
  logisticsNo: [{ required: true, message: '配送物流单号不能为空', trigger: 'blur' }],
  estimatedArrivalTime: [{ required: true, message: '预计到货时间不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '订单状态不能为空', trigger: 'blur' }],
  deliveryStatus: [{ required: true, message: '配送状态不能为空', trigger: 'blur' }],
  alertStatus: [{ required: true, message: '预警状态不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OrderApi.getOrder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as Order
    if (formType.value === 'create') {
      await OrderApi.createOrder(data)
      message.success(t('common.createSuccess'))
    } else {
      await OrderApi.updateOrder(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    orderNo: undefined,
    farmId: undefined,
    storeId: undefined,
    totalPrice: undefined,
    vehiclePlate: undefined,
    logisticsCompany: undefined,
    logisticsNo: undefined,
    estimatedArrivalTime: undefined,
    actualDepartureTime: undefined,
    actualArrivalTime: undefined,
    status: undefined,
    deliveryStatus: undefined,
    alertStatus: undefined,
    alertTime: undefined,
    remark: undefined,
  }
  formRef.value?.resetFields()
}
</script>